const Mock = require('mockjs')

let FreeFiles = []

let id = 1
const typeArr = ['app', 'clueCard', 'movieAssist', 'movieCover', 'movieScene', 'playRoleBook', 'playRole']
const extArr = ['dll', 'png', 'jpg', 'ini', 'txt', 'exe']
for (let i = 0; i < 86; ++i) {
  FreeFiles.push(Mock.mock({
    id: id++,
    fileName: Mock.Random.string() + '.' + extArr[i % extArr.length],
    type: typeArr[i % typeArr.length],
    fileDeleted: Mock.Random.integer(0, 1),
    length: Mock.Random.integer(1, 10 * 1024 * 1024 * 1024)
  }))
}

function list(page, limit, type) {
  limit = Math.max(1, Math.min(parseInt(limit), 50))
  page = Math.max(parseInt(page), 1)
  let mocks = FreeFiles.filter(item => {
    return type && type === item.type
  })
  const total = mocks.length
  mocks = mocks.filter((u, index) => index < limit * page && index >= limit * (page - 1))
  mocks = mocks.map(d => {
    return { ...d }
  })
  return { success: true, data: { total: total, records: mocks }}
}

function deleteRecord(id) {
  id = id ? parseInt(id) : null
  if (id) {
    FreeFiles = FreeFiles.filter(item => item.id !== id)
  }
}

module.exports = { list, deleteRecord }
